import { Module } from '@nestjs/common'
import { UploadService } from './upload.service'
import { UploadController } from './upload.controller'
import { MulterModule } from '@nestjs/platform-express'
import { diskStorage } from 'multer'
import { extname } from 'path'

@Module({
	imports: [
		MulterModule.registerAsync({
			useFactory() {
				return {
					storage: diskStorage({
						// 文件保存路径
						destination: 'uploads/' + new Date().toISOString().substring(0, 7).replace('-', '') + '/',
						filename: (req, file, callback) => {
							const path = Date.now() + '-' + Math.round(Math.random() * 1e4) + extname(file.originalname)
							callback(null, path)
						}
					})
				}
			}
		})
	],
	controllers: [UploadController],
	providers: [UploadService]
})
export class UploadModule {}
